<?php

class Eprof extends Controller {

    function index() {
        $this->set('title', 'YOU - Edit Profile');
        global $session;
        global $imgedit;
        $user_id = $session->get('userid');
        $result = $this->EprofModel->query('SELECT * FROM user WHERE idUser=\''.mysql_real_escape_string($user_id).'\'');
        $nama = $result[0]['User']['fullname'];
        $birthday = $result[0]['User']['tgllahir'];
        $email = $result[0]['User']['email'];
        $gender = $result[0]['User']['gender'];
        $location = $result[0]['User']['kota'];
        $profpic = $result[0]['User']['pp'];
        $this->set('user',$nama);
        $this->set('pbirthday',$birthday);
        $this->set('pemail',$email);
        $this->set('pgender',$gender);
        $this->set('plocation',$location);
    }

    function editprofile() {
        global $session;
        $user_id = $session->get('userid');
        $result = $this->EprofModel->query('SELECT * FROM user WHERE idUser=\''.mysql_real_escape_string($user_id).'\'');
        $field = array();
        $field['username'] = $result[0]['User']['username'];
        $field['fullname'] = $_POST['fullname'];
        $field['password'] = $_POST['password'];
        $field['confpassword'] = $_POST['confirmpwd'];
        $field['tgllahir'] = $_POST['tgl_lahir'];
        $field['email'] = $_POST['email'];
        $field['kota'] = $_POST['kota'];
        $field['gender'] = $_POST['gender'];
        $field['pp'] = $_FILES['profpic'];
        if($field['password'] != $field['confpassword']){
            $this->_auto_render = false;
            $this->redirect(BASE_PATH.'profile/index');
        } else {
            //print_r($field);
            $this->EprofModel->update($field);
            $this->_auto_render = false;
            $this->redirect(BASE_PATH.'profile/index');
        }
    }

    /* 	function register(){
      $field = array();
      $field['fullname'] = $_POST['fnama'];
      $field['username'] = $_POST['fuser'];
      $field['password'] = $_POST['fpass'];
      $field['tgllahir'] = $_POST['ftanggal'];
      $field['email'] = $_POST['femail'];
      $field['kota'] = $_POST['flokasi'];
      $field['gender'] = $_POST['fgender'];
      $field['pp'] = $_FILES['fprofpict'];
      $result = $this->HomeModel->query('select username from user where Username=\''.mysql_real_escape_string($field['username']).'\'');
      if(count($result) != 0){
      //			$this->set('title','Failed');
      $this->set('field',null);
      } else {
      $this->HomeModel->insert($field);
      //global $session;
      //$session->set('userid',$result[0]['User']['idUser']);
      //$this->set('title',$result[0]['User']['fullname']);
      $this->set('field',$field);
      }
      }

      function login(){

      //jangan lupa kalo mau make variabel $session harus definisiin "global $session" soalnya dia static
      global $session;

      $field = array();
      $field['username'] = $_POST['luser'];
      $field['password'] = $_POST['lpass'];

      $result = $this->HomeModel->query('select * from user where Username=\''.mysql_real_escape_string($field['username']).'\' and password=\''.mysql_real_escape_string($field['password']).'\'');
      //$res = mysql_fetch_array($result[0]);
      //print_r(count($result));
      if(count($result) != 0){
      //success login
      //print_r("Login berhasil");
      $this->redirect('http://localhost/YOU/home/index');
      //$this->set('title',$result[0]['User']['fullname']);
      //ini ngeset variabel session dengan nama userid sesuai dengan value di parameter kedua
      //$session->set('userid',$result[0]['User']['idUser']);
      //ini buat dapetin value yang disimpan di session dengan nama userid
      //$session->get('userid');
      //fungsi lainnya lo liat di folder application/session.classs.php intinya kalo mau make harus deklarasiin global $session dulu di fungsi
      //print_r($session);
      }else{
      //failed login
      $this->_auto_render = false;
      //$this->set('title','Failed');
      $this->redirect('http://localhost/YOU/home/index');
      }
      $this->_auto_render = false;
      //                $this->redirect('http://localhost/home/index');
      //                $this->redirect('index');
      } */

    function beforeAction() {
        $this->set('title',null);
    }

}
